package com.nd.android.musicboxlibrary.ui.adapter;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.common.utils.StarAppUtils;

import com.common.StarAppConfig;
import com.nd.android.musicboxlibrary.R;
import com.nd.android.musicboxlibrary.business.bean.MusicInfo;
import com.nd.android.musicboxlibrary.business.comm.BusProvider;
import com.nd.android.musicboxlibrary.business.comm.MenuPositionEvent;
import com.nd.android.musicboxlibrary.interf.ManagerFactory;
import com.nd.android.musicboxlibrary.ui.common.DownloadSelectDialogUtil;
import com.nd.android.musicboxlibrary.ui.common.UnMixable;
import com.nd.android.musicboxlibrary.ui.vip.VipUtil;
import com.nd.android.musicboxlibrary.util.Network;
import com.nd.android.musicboxlibrary.util.ToastUtil;

/**
 * 
 * <br>
 * Created 2014年9月15日 下午11:08:06
 * 
 * @version
 * @author wat.ztag
 * 
 * @see
 */
public class JayMusicSpecialInfoAdapter extends AbstractAdapter<MusicInfo, SpecialInfoViewHolder>
implements OnItemClickListener {

	private HashMap<Integer, Boolean> state = new HashMap<Integer, Boolean>();

	/**
	 * Creates a new instance of JayMusicSpecialInfoAdapter. <br>
	 * Created 2014年9月15日 下午11:08:23
	 * 
     * @param context Context
	 */
	public JayMusicSpecialInfoAdapter(Context context) {
		super(context);
	}

	@Override
	protected SpecialInfoViewHolder getNewHolder() {
		return new SpecialInfoViewHolder();
	}

	@Override
	protected int getItemLayout() {
		return R.layout.mbx_jay_music_single_item;
	}

	@Override
	public void bindView(final SpecialInfoViewHolder holder, final MusicInfo data,
			final int position) {
		holder.txtName.setText(data.getMusicName());
		holder.txtSinger.setText(data.obtainMusicInfo(mContext));
		holder.txtMV.setVisibility(data.isHasMv() ? View.VISIBLE : View.GONE);
		if(StarAppConfig.getBooleanConfig(StarAppConfig.CONFIG_VERSION1_8)){
			//有HQid ,hq文件以及下载完了，再显示HQ标志 by jiangwb
			if(data.getHQId() > 0 && VipUtil.fileExist(data.getHQMusicPath())){
				holder.txtHQ.setBackgroundColor(mContext.getResources().getColor(R.color.mbx_font_lyric_pink));           
			}else{
				holder.txtHQ.setBackgroundColor(mContext.getResources().getColor(R.color.mbx_font_lyric_gray));    
			}
		}else{
			holder.txtHQ.setVisibility(data.isHQ() ? View.VISIBLE : View.GONE);
		}
		holder.cbExpandedMenu.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
				if (arg1) {
					state.clear();
					state.put(position, arg1);
					holder.pnlExpandedMenu.setVisibility(View.VISIBLE);
					notifyDataSetChanged();
					BusProvider.getInstance().post(new MenuPositionEvent(position));
				} else {
					holder.pnlExpandedMenu.setVisibility(View.GONE);
					state.remove(position);
					notifyDataSetChanged();
				}
			}
		});
		holder.cbExpandedMenu.setChecked(state.get(position) == null ? false : true);
		holder.pnlDownload.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				//by jiangwb
				if(StarAppConfig.getBooleanConfig(StarAppConfig.CONFIG_VERSION1_8)){
					DownloadSelectDialogUtil.showDownloadDlg(mContext, VipUtil.isVip(), 
							data,
							new DownloadSelectDialogUtil.OnClickListener() {
						@Override
						public void onRightBtnClicked(boolean isHQ) {
							//确认下载，加入到下载列表中
							if (!Network.isNetworkAvaliable(mContext)) {
								ToastUtil.showLong(mContext, R.string.mbx_no_net_for_download, false);
								ManagerFactory.getDownloadInstance(getContext()).addMusic2DownloadList(data,isHQ);
							} else {
								boolean isSDExitHQ = false; //sd卡是否保存当前歌曲对应的HQ音质的文件
								boolean isSDExitNo = false;//sd卡是否保存当前歌曲对应的普通音质的文件
								if(!TextUtils.isEmpty(data.getHQMusicPath())){
									isSDExitHQ = new File(data.getHQMusicPath()).exists();
								}
								if(!TextUtils.isEmpty(data.getMusicPath())){
									isSDExitNo = new File(data.getMusicPath()).exists();
								}
								// 还未加入下载列表
								if(data.isDownLoading()){//歌曲正在下载中。。。
									ToastUtil.showLong(mContext, R.string.mbx_jaymusic_download_ing, false);
								}else{
									if(isHQ == false && data.isDownLoaded() &&   isSDExitNo){
										//普通歌曲已经下载,用户不需要在下载普通歌曲了
										ToastUtil.showLong(mContext, R.string.mbx_jaymusic_download_done, false);
									}else if( isSDExitHQ && data.isDownLoaded()){
										//HQ歌曲已经下载,用户不需要在下载HQ歌曲了,因为有HQ文件了，所以，也不能下载普通的了
										ToastUtil.showLong(mContext, R.string.mbx_jaymusic_download_done, false);
									}else{
										ToastUtil.showLong(mContext, R.string.mbx_jaymusic_download, false);
										ManagerFactory.getDownloadInstance(getContext()).addMusic2DownloadList(data,isHQ);
									}
								}
							}
						}
					});
				}else{
					if (holder.ivDownload.isSelected()) {
						ToastUtil.showLong(mContext, R.string.mbx_jaymusic_download_done, false);
					} else {
						if (!Network.isNetworkAvaliable(mContext)) {
							ToastUtil.showLong(mContext, R.string.mbx_no_net_for_download, false);
							ManagerFactory.getDownloadInstance(getContext()).addMusic2DownloadList(data,false);
						} else {
							if (data.isDownLoading()) {
								ToastUtil.showLong(mContext, R.string.mbx_jaymusic_download_ing, false);
							} else {
								ToastUtil.showLong(mContext, R.string.mbx_jaymusic_download, false);
								ManagerFactory.getDownloadInstance(getContext()).addMusic2DownloadList(data,false);
							}
						}
					}
				}
			}
		});
		holder.pnlShare.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
                Toast.makeText(mContext, mContext.getString(R.string.mbx_will_finish),
                        Toast.LENGTH_LONG).show();
			}
		});
		holder.pnlPlayer.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
                Toast.makeText(mContext, mContext.getString(R.string.mbx_will_finish),
                        Toast.LENGTH_LONG).show();
			}
		});
		holder.ivDownload.setSelected(data.isDownLoaded());
        holder.txtDownload.setText(data.isDownLoaded() ? R.string.mbx_downloaded
                : R.string.mbx_download);
		holder.pnlExpandedMenu.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
			}
		});
		holder.pnlLike.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (data.isFaved()) {
					showDialog(data);
				} else {
					data.setFaved(true);
					ManagerFactory.getPlaylistInstance(getContext()).likeMusic(data);
				}
				notifyDataSetChanged();
			}
		});
		holder.ivLike.setImageResource(data.isFaved() ? R.drawable.mbx_btn_unlike_selector
				: R.drawable.mbx_btn_like_selector);
		holder.txtLike.setText(data.isFaved() ? R.string.mbx_expanded_cancel_fav
				: R.string.mbx_expanded_join_fav);
        StarAppUtils.setViewTypeFace(holder.txtHQ, false);
        StarAppUtils.setViewTypeFace(holder.txtLike, false);
        StarAppUtils.setViewTypeFace(holder.txtTime, false);
        StarAppUtils.setViewTypeFace(holder.txtName, false);
        StarAppUtils.setViewTypeFace(holder.txtMV, false);
        StarAppUtils.setViewTypeFace(holder.txtDownload, false);
        StarAppUtils.setViewTypeFace(holder.txtSinger, false);
        StarAppUtils.setViewTypeFace(holder.txtPlayMV, false);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		ManagerFactory.getPlayerInstance().playBySpecialItem(mContext, getItem(arg2),
				(ArrayList<MusicInfo>) getDatas());
	}

	/**
	 * 
	 * <br>
	 * Created 2014年9月15日 下午11:08:28
	 * 
     * @param data MusicInfo
	 * @author wat.ztag
	 */
	private void showDialog(MusicInfo data) {
		new AlertDialog.Builder(mContext).setTitle(R.string.mbx_hint)
		.setMessage(R.string.mbx_confirm_unlike_music)
		.setPositiveButton(R.string.mbx_confirm, new RemoveListener(data))
		.setNegativeButton(R.string.mbx_cancel, null).show();
	}

	/**
	 * 
	 * <br>
	 * Created 2014年9月15日 下午11:08:40
	 * 
	 * @version JayMusicSpecialInfoAdapter
	 * @author wat.ztag
	 * 
	 * @see
	 */
	private class RemoveListener implements android.content.DialogInterface.OnClickListener {

		private MusicInfo mData;

		/**
		 * Creates a new instance of RemoveListener. <br>
		 * Created 2014年9月15日 下午11:08:44
		 * 
         * @param data MusicInfo
		 */
		public RemoveListener(MusicInfo data) {
			this.mData = data;
		}

		@Override
		public void onClick(DialogInterface arg0, int arg1) {
			mData.setFaved(false);
			ManagerFactory.getPlaylistInstance(getContext()).unLikeMusic(mData);
			arg0.dismiss();
			notifyDataSetChanged();
		}
	}

}

/**
 * 
 * <br>
 * Created 2014年9月15日 下午11:08:47
 * 
 * @version
 * @author wat.ztag
 * 
 * @see
 */
class SpecialInfoViewHolder implements UnMixable {
	TextView txtName;
	TextView txtSinger;
	TextView txtTime;
	TextView txtMV;
	TextView txtHQ;
	CheckBox cbExpandedMenu;
	LinearLayout pnlExpandedMenu;
	LinearLayout pnlDownload, pnlShare, pnlPlayer, pnlLike;
	ImageView ivDownload, ivLike;
	TextView txtLike;
	TextView txtDownload;
    TextView txtPlayMV;
}
